package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.storage.domain.PickingOutsourceDetail;
import com.pureut.storage.dto.PickingOutsourceDetailDto;
import com.pureut.storage.mapper.PickingOutsourceDetailMapper;
import com.pureut.storage.service.PickingOutsourceDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.dict.utils.DictUtils;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2023/1/31  9:57
 */
@Service
public class PickingOutsourceDetailServiceImpl extends ServiceImpl<PickingOutsourceDetailMapper, PickingOutsourceDetail> implements PickingOutsourceDetailService {

    @Resource
    PickingOutsourceDetailMapper pickingOutsourceDetailMapper;

    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 委外发货明细列表
     *
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<PickingOutsourceDetailDto> getList(String docNum, PageQuery pageQuery) {
        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_outsource_detail_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        Page<PickingOutsourceDetailDto> list = pickingOutsourceDetailMapper.getPickDetailList(docNum, pageQuery.build());
        List<PickingOutsourceDetailDto> records = list.getRecords();
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        for (PickingOutsourceDetailDto pickingOutsourceDetailDto : records) {
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + pickingOutsourceDetailDto.getTenantId(), String.valueOf(pickingOutsourceDetailDto.getMaterialId()));
            pickingOutsourceDetailDto.setAuxiliaryAttribute(statsArray);
            //单位
            if (unitByCodeToMap.get(String.valueOf(pickingOutsourceDetailDto.getMaterialUnit())) != null) {
                pickingOutsourceDetailDto.setMaterialUnitDict(unitByCodeToMap.get(String.valueOf(pickingOutsourceDetailDto.getMaterialUnit())).getUnitName());
            }
            pickingOutsourceDetailDto.setStatusDict(pickingOutsourceDetailDto.getStatus());
            pickingOutsourceDetailDto.setStatus(statusMap.get(pickingOutsourceDetailDto.getStatus()));
        }
        return TableDataInfo.build(list);
    }
}
